home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / earcd / util / misc / man_perl.lha / man.perl
Text File  |  1997-01-10  |  3KB  |  137 lines

  1. #!perl
  2.  
  3. # This is a replacement 'man' command written in perl.
  4. # It requires Perl 5 although it shouldn't be to hard to rewrite it for perl 4
  5. # groff is also warmly recommended
  6. # It does not use any special configuration files but just looks through the 
  7. # directories in the environment variable MANPATH.
  8. # In each dir mention in $MANPATH it looks for dirnames of the
  9. # type manx/catx where x is the section "number" ( x can by just about
  10. # any character). The files in manx is nroff style documents while
  11. # those in catx are preformatted. If the file filename.whatever in manx
  12. # is newer than the file filename.0 in catx the file in manx automatically
  13. # gets processed. (No need to explicitly call [g]nroff for man-files anymore)
  14. # This code is hereby placed into public domain.
  15.  
  16.  
  17. use Getopt::Std;
  18.  
  19.  
  20.  
  21. getopts('as:M:m:');
  22.  
  23. $fname=shift @ARGV;
  24. if(! $fname )
  25.     {
  26.     print "$0 [-a] [-s section] [-M path] [-m path] name\n";
  27.     exit 0;
  28.     }
  29.     
  30. $manpath=$ENV{'MANPATH'};
  31.  
  32. if(! $manpath )
  33.     {
  34.     $manpath='/usr/man/';
  35.     }
  36.     
  37. $pager=$ENV{'PAGER'};
  38. if(! $pager)
  39.     {
  40.     $pager='less -s';
  41.     }
  42.  
  43.  
  44. if($opt_M)
  45.     {
  46.     $manpath=$opt_M;
  47.     }
  48.     
  49. if($opt_m)
  50.     {
  51.     $manpath= $opt_m . ":" . $manpath;
  52.     }
  53.     
  54.     
  55.     
  56.     
  57. @mandirs=split(/:/,$manpath);
  58.  
  59. foreach $mand ( @mandirs )
  60.     {
  61.     if(!opendir(MDIR,$mand) ) { print "Can't find directory $mand \n"; next ;}
  62.     @allfiles=readdir(MDIR);
  63.     closedir(MDIR);
  64.     if($opt_s)
  65.         {
  66.         @mdirs=grep( /^man$opt_s$/ , @allfiles );
  67.         }
  68.     else
  69.         {
  70.         @mdirs=grep(  /^man.$/ , @allfiles);
  71.         }
  72.     foreach $md ( @mdirs )
  73.         {
  74.         if(! -d "$mand/$md" ) { next ; }
  75.         $mext=substr($md,3);
  76.         opendir(MD,"$mand/$md") || die;
  77.         @mfiles=readdir(MD);
  78.         closedir(MD);
  79.         @mfiles=grep( /^\Q$fname\E\.[^.]*$/ , @mfiles);
  80.         $mf="$mand/$md/" . $mfiles[0];
  81.         $cf="$mand/cat$mext/" . $fname . ".0";
  82.         if( -d "$mand/cat$mext" )
  83.             {
  84.             if( -f $mf && -s $mf)
  85.                 {
  86.                 if( -f $cf && -s $cf)
  87.                     {
  88.                     $ca=(stat($cf))[9];
  89.                     $ma=(stat($mf))[9];
  90.                     if ($ma > $ca )
  91.                         {
  92.                         print "Reformatting page...\n";
  93.                         system("groff -t -Tascii -mandoc $mf > $cf");
  94.                         }
  95.                     }
  96.                 else
  97.                     {
  98.                     print "Reformatting page...\n";
  99.                     system("groff -t -Tascii -mandoc $mf > $cf");
  100.                     }
  101.                 }
  102.             if( -f $cf && -s $cf)
  103.                 {
  104.                 if($opt_a)
  105.                     {
  106.                     system("$pager $cf");
  107.                     }
  108.                 else
  109.                     {
  110.                     exec("$pager $cf");
  111.                     die "Can't execute $pager\n";
  112.                     }
  113.                 }
  114.             }
  115.         else
  116.             {
  117.             if( -f $mf && -s $mf)
  118.                 {
  119.                 if($opt_a)
  120.                     {
  121.                     system("groff -t -Tascii -mandoc $mf | $pager");
  122.                     }
  123.                 else
  124.                     {
  125.                     exec("groff -t -Tascii -mandoc $mf | $pager");
  126.                     }
  127.                 }    
  128.             }
  129.         }    
  130.     }    
  131.         
  132.  
  133. if(! $opt_a)
  134.     {
  135.     print "Couldn't find manpage\n";
  136.     }
  137.